package queuedemo;
//用数组实现循环队列，标记法
public class MyQueue4 {
    public int[] elem;
    public boolean flg;
    public int front;
    public int rear;
    public MyQueue4(int k) {
        this.elem = new int[k];
    }
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        flg = true;
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        flg = false;
        front = (front +1) % elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        return rear == 0 ? elem[elem.length-1] : elem[rear-1];
    }

    public boolean isEmpty() {
        if(front == rear && flg == false) {
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if(front == rear && flg == true) {
            return true;
        }
        return false;
    }
}
